<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="流程" tabindex="-1"><a class="header-anchor" href="#流程"><span>流程</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#invoking-processes">调用进程</a>
<ul>
<li><a href="#process-options">流程选项</a></li>
<li><a href="#process-output">流程输出</a></li>
<li><a href="#process-pipelines">管道</a></li>
</ul>
</li>
<li><a href="#asynchronous-processes">异步进程</a>
<ul>
<li><a href="#process-ids-and-signals">进程 ID 和信号</a></li>
<li><a href="#asynchronous-process-output">异步进程输出</a></li>
</ul>
</li>
<li><a href="#concurrent-processes">并发进程</a>
<ul>
<li><a href="#naming-pool-processes">命名池进程</a></li>
<li><a href="#pool-process-ids-and-signals">池进程 ID 和信号</a></li>
</ul>
</li>
<li><a href="#testing">测试</a>
<ul>
<li><a href="#faking-processes">伪造进程</a></li>
<li><a href="#faking-specific-processes">伪造特定进程</a></li>
<li><a href="#faking-process-sequences">伪造进程序列</a></li>
<li><a href="#faking-asynchronous-process-lifecycles">伪造异步进程生命周期</a></li>
<li><a href="#available-assertions">可用断言</a></li>
<li><a href="#preventing-stray-processes">防止杂散进程</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 围绕 <a href="https://symfony.com/doc/7.0/components/process.html" target="_blank" rel="noopener noreferrer">Symfony Process 组件</a> 提供了富有表现力且精简的 API，让你可以方便地从 Laravel 应用程序调用外部进程。Laravel 的进程功能专注于最常见的用例和出色的开发人员体验。</p>
<h2 id="调用进程" tabindex="-1"><a class="header-anchor" href="#调用进程"><span>调用进程</span></a></h2>
<p>要调用进程，你可以使用 <code v-pre>Process</code> 门面提供的 <code v-pre>run</code> 和 <code v-pre>start</code> 方法。 <code v-pre>run</code> 方法将调用进程并等待进程完成执行，而 <code v-pre>start</code> 方法用于异步进程执行。我们将在本文档中研究这两种方法。首先，让我们研究如何调用基本的同步进程并检查其结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token variable">$result</span><span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，<code v-pre>run</code> 方法返回的 <code v-pre>Illuminate\Contracts\Process\ProcessResult</code> 实例提供了多种有用的方法，可用于检查进程结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span><span class="token operator">-></span><span class="token function">successful</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$result</span><span class="token operator">-></span><span class="token function">failed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$result</span><span class="token operator">-></span><span class="token function">exitCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$result</span><span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$result</span><span class="token operator">-></span><span class="token function">errorOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="抛出异常" tabindex="-1"><a class="header-anchor" href="#抛出异常"><span>抛出异常</span></a></h4>
<p>如果你有一个进程结果，并且希望在退出代码大于零（从而表示失败）时抛出 <code v-pre>Illuminate\Process\Exceptions\ProcessFailedException</code> 实例，则可以使用 <code v-pre>throw</code> 和 <code v-pre>throwIf</code> 方法。如果进程没有失败，则将返回进程结果实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">throw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">throwIf</span><span class="token punctuation">(</span><span class="token variable">$condition</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="流程选项" tabindex="-1"><a class="header-anchor" href="#流程选项"><span>流程选项</span></a></h3>
<p>当然，在调用进程之前，你可能需要自定义进程的行为。刚好 Laravel 允许你调整各种进程功能，例如工作目录、超时和环境变量。</p>
<h4 id="工作目录路径" tabindex="-1"><a class="header-anchor" href="#工作目录路径"><span>工作目录路径</span></a></h4>
<p>你可以使用 <code v-pre>path</code> 方法指定进程的工作目录。如果不调用此方法，进程将继承当前正在执行的 PHP 脚本的工作目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="输入" tabindex="-1"><a class="header-anchor" href="#输入"><span>输入</span></a></h4>
<p>你可以使用 <code v-pre>input</code> 方法通过进程的「标准输入」提供输入：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cat'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="超时" tabindex="-1"><a class="header-anchor" href="#超时"><span>超时</span></a></h4>
<p>默认情况下，进程执行超过 60 秒后将抛出 <code v-pre>Illuminate\Process\Exceptions\ProcessTimedOutException</code> 实例。但是，你可以通过 <code v-pre>timeout</code> 方法自定义此行为：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">120</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，你可以调用 <code v-pre>forever</code> 方法完全禁用进程超时：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">forever</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>idleTimeout</code> 方法可用于指定进程在不返回任何输出的情况下运行的最大秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">idleTimeout</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="环境变量" tabindex="-1"><a class="header-anchor" href="#环境变量"><span>环境变量</span></a></h4>
<p>可以通过 <code v-pre>env</code> 方法向进程提供环境变量。调用的进程还将继承系统定义的所有环境变量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">forever</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">env</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'IMPORT_PATH'</span> <span class="token operator">=></span> <span class="token constant">__DIR__</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想从调用的进程中删除继承的环境变量，可以为该环境变量提供 <code v-pre>false</code> 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">forever</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">env</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'LOAD_PATH'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="tty-模式" tabindex="-1"><a class="header-anchor" href="#tty-模式"><span>TTY 模式</span></a></h4>
<p><code v-pre>tty</code> 方法可用于为进程启用 TTY 模式。TTY 模式将进程的输入和输出连接到程序的输入和输出，允许进程以进程形式打开 Vim 或 Nano 等编辑器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">forever</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">tty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vim'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="进程输出" tabindex="-1"><a class="header-anchor" href="#进程输出"><span>进程输出</span></a></h3>
<p>如前所述，可以使用进程结果上的 <code v-pre>output</code> (stdout) 和 <code v-pre>errorOutput</code> (stderr) 方法访问进程输出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$result</span><span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$result</span><span class="token operator">-></span><span class="token function">errorOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>但是，也可以通过将闭包作为第二个参数传递给 <code v-pre>run</code> 方法，实时收集输出。闭包将接收两个参数：输出的 「类型」（<code v-pre>stdout</code> 或 <code v-pre>stderr</code>）和输出字符串本身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$output</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$output</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Laravel 还提供了 <code v-pre>seeInOutput</code> 和 <code v-pre>seeInErrorOutput</code> 方法，它们提供了一种方便的方法来确定给定的字符串是否包含在进程的输出中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">seeInOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="禁用流程输出" tabindex="-1"><a class="header-anchor" href="#禁用流程输出"><span>禁用流程输出</span></a></h4>
<p>如果你的进程正在写入大量你不感兴趣的输出，你可以通过完全禁用输出检索来节省内存。为此，请在构建进程时调用 <code v-pre>quietly</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">quietly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="管道" tabindex="-1"><a class="header-anchor" href="#管道"><span>管道</span></a></h3>
<p>有时候你希望将一个进程的输出作为另一个进程的输入。这通常称为将一个进程的输出「管道化」到另一个进程中。<code v-pre>Process</code> 门面提供的 <code v-pre>pipe</code> 方法可以轻松实现此目的。<code v-pre>pipe</code> 方法将同步执行管道进程并返回管道中最后一个进程的进程结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Process<span class="token punctuation">\</span>Pipe</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pipe</span> <span class="token variable">$pipe</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$pipe</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cat example.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pipe</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'grep -i "laravel"'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$result</span><span class="token operator">-></span><span class="token function">successful</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你不需要定制组成管道的各个进程，你可以简单地将命令字符串数组传递给 <code v-pre>pipe</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'cat example.txt'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'grep -i "laravel"'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通过将闭包作为第二个参数传递给 <code v-pre>pipe</code> 方法，可以实时收集进程输出。闭包将接收两个参数：输出的 「类型」 （<code v-pre>stdout</code> 或 <code v-pre>stderr</code>）和输出字符串本身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pipe</span> <span class="token variable">$pipe</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$pipe</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cat example.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pipe</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'grep -i "laravel"'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$output</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$output</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Laravel 还允许你通过 <code v-pre>as</code> 方法为管道中的每个进程分配字符串键。此键还将传递给 <code v-pre>pipe</code> 方法提供的输出闭包，让你可以确定输出属于哪个进程：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pipe</span> <span class="token variable">$pipe</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$pipe</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cat example.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pipe</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'second'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'grep -i "laravel"'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$output</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="异步进程" tabindex="-1"><a class="header-anchor" href="#异步进程"><span>异步进程</span></a></h2>
<p>虽然 <code v-pre>run</code> 方法同步调用进程，但可以使用 <code v-pre>start</code> 方法异步调用进程。这使得你的应用程序能够在进程后台运行时继续执行其他任务。一旦进程被调用，你可以使用 <code v-pre>running</code> 方法来判断进程是否仍在运行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$process</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">120</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$process</span><span class="token operator">-></span><span class="token function">running</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所料，你可以调用 <code v-pre>wait</code> 方法来等待进程执行完毕并获取进程结果实例</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$process</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">120</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="进程-id-和信号" tabindex="-1"><a class="header-anchor" href="#进程-id-和信号"><span>进程 ID 和信号</span></a></h3>
<p>可以使用 <code v-pre>id</code> 方法来获取正在运行的进程的操作系统分配的进程 ID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$process</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>signal</code> 方法向正在运行的进程发送「信号」。可以在 <a href="https://www.php.net/manual/en/pcntl.constants.php" target="_blank" rel="noopener noreferrer">PHP 文档</a> 中找到预定义信号常量列表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$process</span><span class="token operator">-></span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token constant">SIGUSR2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="异步过程输出" tabindex="-1"><a class="header-anchor" href="#异步过程输出"><span>异步过程输出</span></a></h3>
<p>当异步进程运行时，你可以使用 <code v-pre>output</code> 和 <code v-pre>errorOutput</code> 方法访问其当前的全部输出；也可以使用 <code v-pre>latestOutput</code> 和 <code v-pre>latestErrorOutput</code> 来访问上一次获取输出以来发生的进程输出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$process</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">120</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$process</span><span class="token operator">-></span><span class="token function">running</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token function">latestOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token function">latestErrorOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>与 <code v-pre>run</code> 方法类似，也可以通过将闭包作为第二个参数传递给 <code v-pre>start</code> 方法，实时收集异步进程的输出。闭包将接收两个参数：输出的 「类型」 （<code v-pre>stdout</code> 或 <code v-pre>stderr</code>）和输出字符串本身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$process</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$output</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$output</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="并发进程" tabindex="-1"><a class="header-anchor" href="#并发进程"><span>并发进程</span></a></h2>
<p>Laravel 还使管理并发的异步进程池变得轻而易举，让你能够轻松地同时执行多个任务。首先，调用 <code v-pre>pool</code> 方法，该方法接受一个闭包，该闭包接收一个 <code v-pre>Illuminate\Process\Pool</code> 实例。</p>
<p>在这个闭包中，你可以定义属于该池的进程。一旦通过 <code v-pre>start</code> 方法启动了进程池，你就可以通过 <code v-pre>running</code> 方法访问正在运行的进程的 <a href="https://learnku.com/docs/laravel/11.x/collections" target="_blank" rel="noopener noreferrer">集合</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Process<span class="token punctuation">\</span>Pool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$pool</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">pool</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pool</span> <span class="token variable">$pool</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import-1.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import-2.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import-3.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$output</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">running</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，你可以等待池中所有进程执行完成并通过 <code v-pre>wait</code> 方法解析它们的结果。<code v-pre>wait</code> 方法返回一个可访问进程结果实例的数组对象，通过其键可以访问池中每个进程的进程结果实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$results</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，为了方便，可以使用 <code v-pre>concurrently</code> 方法启动一个异步进程池并立即等待其结果。当与 PHP 的数组解构功能结合使用时，还可以提供特别表达式的语法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span><span class="token variable">$first</span><span class="token punctuation">,</span> <span class="token variable">$second</span><span class="token punctuation">,</span> <span class="token variable">$third</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">concurrently</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pool</span> <span class="token variable">$pool</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">app_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$first</span><span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="命名进程池中的进程" tabindex="-1"><a class="header-anchor" href="#命名进程池中的进程"><span>命名进程池中的进程</span></a></h3>
<p>通过数字键访问进程池结果不太具有表达性，因此 Laravel 允许你通过 <code v-pre>as</code> 方法为池中的每个进程分配字符串键。这个键也将传递给提供给 <code v-pre>start</code> 方法的闭包，使你能够确定输出属于哪个进程:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$pool</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">pool</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pool</span> <span class="token variable">$pool</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import-1.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'second'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import-2.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'third'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import-3.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$type</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$output</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token variable">$results</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="进程池进程-id-和信号" tabindex="-1"><a class="header-anchor" href="#进程池进程-id-和信号"><span>进程池进程 ID 和信号</span></a></h3>
<p>由于进程池的 <code v-pre>running</code> 方法提供了一个包含池中所有已调用进程的集合，因此你可以轻松地访问基础池进程的 ID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$processIds</span> <span class="token operator">=</span> <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">running</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">each</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>为了方便，您可以在进程池上调用 <code v-pre>signal</code> 方法向池中的每个进程发送信号：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token constant">SIGUSR2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="测试" tabindex="-1"><a class="header-anchor" href="#测试"><span>测试</span></a></h2>
<p>许多 Laravel 服务都提供功能，以帮助你轻松、有表达力地编写测试，Laravel 的进程服务也不例外。<code v-pre>Process</code> 门面的 <code v-pre>fake</code> 方法允许你指示 Laravel 在调用进程时返回存根 / 伪造结果。</p>
<h3 id="伪造进程" tabindex="-1"><a class="header-anchor" href="#伪造进程"><span>伪造进程</span></a></h3>
<p>在探索 Laravel 的伪造进程能力时，让我们假设一下调用进程的路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Route</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/import'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'Import complete!'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在测试此路由时，我们可以通过在 <code v-pre>Process</code> 门面上调用无参数的 <code v-pre>fake</code> 方法，让 Laravel 返回每个伪造的成功进程结果。此外，我们甚至可以 <a href="#available-assertions">断言</a> 某个进程 “已运行”:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Process<span class="token punctuation">\</span>PendingProcess</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Process<span class="token punctuation">\</span>ProcessResult</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'process is invoked'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/import'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 简单的进程断言...</span></span>
<span class="line">    <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 或者，检查进程配置...</span></span>
<span class="line">    <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRan</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">PendingProcess</span> <span class="token variable">$process</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">ProcessResult</span> <span class="token variable">$result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">command</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'bash import.sh'</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">               <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">timeout</span> <span class="token operator">===</span> <span class="token number">60</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Feature</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Process<span class="token punctuation">\</span>PendingProcess</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Process<span class="token punctuation">\</span>ProcessResult</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_process_is_invoked</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/import'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 简单的进程断言...</span></span>
<span class="line">        <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 或者，检查进程配置...</span></span>
<span class="line">        <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRan</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">PendingProcess</span> <span class="token variable">$process</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">ProcessResult</span> <span class="token variable">$result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">command</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'bash import.sh'</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">                   <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">timeout</span> <span class="token operator">===</span> <span class="token number">60</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所述，在 <code v-pre>Process</code> 门面上调用 <code v-pre>fake</code> 方法将指示 Laravel 始终返回没有输出的成功进程结果。然而，你可以使用 <code v-pre>Process</code> 门面的 <code v-pre>result</code> 方法为伪造的进程指定输出和退出代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">result</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">output</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Test output'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">errorOutput</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Test error output'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token argument-name">exitCode</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="伪造特定进程" tabindex="-1"><a class="header-anchor" href="#伪造特定进程"><span>伪造特定进程</span></a></h3>
<p>如之前的示例中所提到的 <code v-pre>Process</code> 门面允许您通过传递一个数组到 <code v-pre>fake</code> 方法来为不同的进程指定不同的假结果。</p>
<p>数组的键应该表示你想伪造的命令模式及其相关结果。星号 <code v-pre>*</code> 字符可用作通配符，任何未被伪造的进程命令将会被实际执行。你可以使用 <code v-pre>Process</code> 门面的 <code v-pre>result</code> 方法为这些命令构建 stub / fake 结果:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'cat *'</span> <span class="token operator">=></span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">result</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">output</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Test "cat" output'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'ls *'</span> <span class="token operator">=></span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">result</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token argument-name">output</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'Test "ls" output'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您不需要自定义伪造进程的退出代码或错误输出，使用简单的字符串指定伪造的进程结果可能会更加方便：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'cat *'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Test "cat" output'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'ls *'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Test "ls" output'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="伪造进程序列" tabindex="-1"><a class="header-anchor" href="#伪造进程序列"><span>伪造进程序列</span></a></h3>
<p>如果您正在测试的代码多次使用相同的命令调用多个进程，您可能希望为每次进程调用分配不同的伪造进程结果。您可以通过 <code v-pre>Process</code> 门面的 <code v-pre>sequence</code> 方法来实现这一点：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'ls *'</span> <span class="token operator">=></span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">sequence</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">result</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'First invocation'</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">result</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Second invocation'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="伪造异步进程的生命周期" tabindex="-1"><a class="header-anchor" href="#伪造异步进程的生命周期"><span>伪造异步进程的生命周期</span></a></h3>
<p>到目前为止，我们主要讨论了伪造使用 <code v-pre>run</code> 方法同步调用的伪造进程。但是，如果你正在尝试测试与通过 <code v-pre>start</code>调用的异步进程交互的代码，则可能需要更复杂的方法来描述伪造进程。</p>
<p>例如，让我们假设以下路由，它与一个异步进程交互：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Log</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Route</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/import'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$process</span> <span class="token operator">=</span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token variable">$process</span><span class="token operator">-></span><span class="token function">running</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Log</span><span class="token operator">::</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token variable">$process</span><span class="token operator">-></span><span class="token function">latestOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token class-name static-context">Log</span><span class="token operator">::</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token variable">$process</span><span class="token operator">-></span><span class="token function">latestErrorOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'Done'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为了正确伪造这个进程，我们需要能够描述 <code v-pre>running</code> 方法应返回 <code v-pre>true</code>的次数。此外，我们可能想要指定多行顺序返回的输出。为了实现这一点，我们可以使用 <code v-pre>Process</code> 门面的 <code v-pre>describe</code> 方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bash import.sh'</span> <span class="token operator">=></span> <span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">describe</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'First line of standard output'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">errorOutput</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'First line of error output'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">output</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Second line of standard output'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">exitCode</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">iterations</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>让我们深入研究上面的示例。使用 <code v-pre>output</code> and <code v-pre>errorOutput</code> 方法，我们可以指定顺序返回的多行输出。<code v-pre>exitCode</code> 方法可用于指定伪造进程的最终退出码。最后， <code v-pre>iterations</code> 方法可用于指定 <code v-pre>running</code> 方法应返回 <code v-pre>true</code> 的次数。</p>
<h3 id="可用的断言" tabindex="-1"><a class="header-anchor" href="#可用的断言"><span>可用的断言</span></a></h3>
<p><a href="#faking-processes">如前所述</a>, Laravel 为你的功能测试提供了几个进程断言。我们将在下面讨论每个断言。</p>
<h4 id="assertran" tabindex="-1"><a class="header-anchor" href="#assertran"><span>assertRan</span></a></h4>
<p>断言调用了给定的进程：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>assertRan</code> 方法还接受一个闭包，该闭包将接收一个进程实例和一个进程结果，允许你检查进程的配置选项。如果此闭包返回 <code v-pre>true</code>, 则断言将 「 通过 」 :</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRan</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$process</span><span class="token punctuation">,</span> <span class="token variable">$result</span><span class="token punctuation">)</span> <span class="token operator">=></span></span>
<span class="line">    <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">command</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'ls -la'</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">    <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">path</span> <span class="token operator">===</span> <span class="token constant">__DIR__</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">    <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">timeout</span> <span class="token operator">===</span> <span class="token number">60</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>传递给 <code v-pre>assertRan</code> 闭包的 <code v-pre>$process</code>是 <code v-pre>Illuminate\Process\PendingProcess</code>的一个实例，而 <code v-pre>$result</code> 是 <code v-pre>Illuminate\Contracts\Process\ProcessResult</code>的一个实例。</p>
<h4 id="assertdidntrun" tabindex="-1"><a class="header-anchor" href="#assertdidntrun"><span>assertDidntRun</span></a></h4>
<p>断言没有调用给定的进程 :</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertDidntRun</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>与 <code v-pre>assertRan</code> 方法类似， <code v-pre>assertDidntRun</code> 方法也接受一个闭包，该闭包将接收一个进程实例和一个进程结果，允许你检查进程的配置选项。如果此闭包返回 <code v-pre>true</code>，则断言将 「 失败 」 ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertDidntRun</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">PendingProcess</span> <span class="token variable">$process</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">ProcessResult</span> <span class="token variable">$result</span><span class="token punctuation">)</span> <span class="token operator">=></span></span>
<span class="line">    <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">command</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'ls -la'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="assertrantimes" tabindex="-1"><a class="header-anchor" href="#assertrantimes"><span>assertRanTimes</span></a></h4>
<p>断言给定的进程被调用了给定的次数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRanTimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">,</span> <span class="token argument-name">times</span><span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>assertRanTimes</code> 方法也接受一个闭包，该闭包将接收一个进程实例和一个进程结果，允许你检查进程的配置选项。如果此闭包返回 <code v-pre>true</code> 且进程被调用了指定次数，则断言将 「 通过 」 ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">assertRanTimes</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">PendingProcess</span> <span class="token variable">$process</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">ProcessResult</span> <span class="token variable">$result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$process</span><span class="token operator">-></span><span class="token property">command</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token argument-name">times</span><span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="防止杂散进程" tabindex="-1"><a class="header-anchor" href="#防止杂散进程"><span>防止杂散进程</span></a></h3>
<p>如果你想确保在单个测试或完整测试套件中所有调用的进程都是伪造的，你可以调用 <code v-pre>preventStrayProcesses</code> 方法。调用此方法后，任何没有相应伪造结果的进程都将抛出异常，而不是启动实际进程：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Process</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">preventStrayProcesses</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'ls *'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Test output...'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 返回了虚假响应...</span></span>
<span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ls -la'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 引发异常...</span></span>
<span class="line"><span class="token class-name static-context">Process</span><span class="token operator">::</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bash import.sh'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/processesmd/16685" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pr...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/processesmd/16685" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pr...</a></p>
</blockquote>
</div></template>


